package com.iotboss.system.service.impl;

import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.iotboss.common.constant.UserConstants;
import com.iotboss.common.core.domain.entity.SysDept;
import com.iotboss.common.core.domain.entity.SysUser;
import com.iotboss.common.core.domain.iot.IotCustom;
import com.iotboss.common.core.domain.iot.IotDeptCompany;
import com.iotboss.common.core.domain.iot.IotOrUser;
import com.iotboss.common.utils.SecurityUtils;
import com.iotboss.common.utils.StringUtils;
import com.iotboss.system.mapper.IotCustomMapper;
import com.iotboss.system.mapper.SysRoleDeptMapper;
import com.iotboss.system.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class IotCustomServiceImpl implements IotCustomService {
    @Autowired
    IotCustomMapper iotCustomMapper;
    @Autowired
    private ISysDeptService deptService;
    @Autowired
    private IotDeptCompanyService companyService;
    @Autowired
    private ISysUserService userService;
    @Autowired
    private IotOrUserService iotOrUserService;
    @Autowired
    private ISysRoleService sysRoleService;
    @Override
    public List<IotCustom> selectIotCustomList(IotCustom iotCustom) {
        return iotCustomMapper.selectIotCustomList(iotCustom);  }

    @Override
    public boolean findIotCustom(IotCustom iotCustom) {
        Long id = StringUtils.isNull(iotCustom.getId()) ? -1L : iotCustom.getId();
        IotCustom info = iotCustomMapper.findIotCustom(iotCustom.getCustomName(), iotCustom.getCustomPhone(),iotCustom.getCompanyName());
        if (StringUtils.isNotNull(info) && info.getId().longValue() != id.longValue())
        {
            return UserConstants.NOT_UNIQUE;
        }
        return UserConstants.UNIQUE;
    }

    @Override
    public int insertIotCustom(IotCustom iotCustom) {
        int i = iotCustomMapper.insertIotCustom(iotCustom);
        SysDept sysDept = new SysDept();
        if (!iotCustom.getCustomPhone().equals("")) {
            //添加客户公司信息并插入到中间表dept_company
            sysDept.setDeptName(iotCustom.getCompanyName());
            sysDept.setLeader(iotCustom.getCustomName());
            sysDept.setPhone(iotCustom.getCustomPhone());

            if (SecurityUtils.getLoginUser().getDeptId().toString().equals("100")){
                sysDept.setParentId(100L);
                sysDept.setCreateBy(SecurityUtils.getLoginUser().getUsername());
                deptService.insertDept(sysDept);
            }else if (!SecurityUtils.getLoginUser().getDeptId().toString().equals("100")){
                sysDept.setCreateBy(SecurityUtils.getLoginUser().getUsername());
                deptService.insertDept(sysDept);
            }
        }
        //添加公司信息同步到公司中间表
        this.deptToCompany(sysDept, iotCustom);

        return i;
    }
    @Override
    public int updateIotCustom(IotCustom iotCustom) {
        return iotCustomMapper.updateIotCustom(iotCustom);
    }

    @Override
    public int deleteIotCustomByIds(Long[] id) {
        return iotCustomMapper.deleteIotCustomByIds(id);
    }

    @Override
    public IotCustom selectIotCustomById(Long id) {
         return iotCustomMapper.selectIotCustomById(id);
    }

    @Override
    public int addCompany(SysDept sysDept) {
        return deptService.insertDept(sysDept);
    }

    private void deptToCompany(SysDept sysDept, IotCustom iotCustom) {
        IotDeptCompany company = new IotDeptCompany();
        if (ObjectUtils.isNotEmpty(sysDept)) {
            company.setCompanyName(sysDept.getDeptName());
            company.setDeptId(sysDept.getDeptId());
            company.setCustomId(iotCustom.getId());
            company.setCreateBy(SecurityUtils.getLoginUser().getUsername());
            companyService.insertCompany(company);
        }
        //添加企业时创建一个管理员用户账号
        SysUser sysUser = new SysUser();
        if (company.getId() != null) {
            sysUser.setCompanyId(company.getId());
        }
        if (sysDept.getDeptId() != null) {
            sysUser.setDeptId(sysDept.getDeptId());
        }
        sysUser.setEmail(sysDept.getEmail());
        sysUser.setNickName(sysDept.getDeptName());
        sysUser.setUserName(sysDept.getPhone());
        Long[] roles = {2L};
        sysUser.setRoleIds(roles);
        sysUser.setPhonenumber(sysDept.getPhone());
        sysUser.setCreateBy(SecurityUtils.getLoginUser().getUsername());
        sysUser.setPassword(SecurityUtils.encryptPassword(sysDept.getPhone()));
        userService.insertUser(sysUser);
        //插入创建用户操作记录表
        IotOrUser iotOrUser = new IotOrUser();
        iotOrUser.setCreateBy(SecurityUtils.getLoginUser().getUsername());
        iotOrUser.setType(0);
        iotOrUser.setDeptId(sysDept.getDeptId());
        iotOrUser.setCustomId(iotCustom.getId());
        iotOrUserService.insertOrUser(iotOrUser);
    }
}
